// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/sparepartshospital/api/SpareApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/sparepartshospital/model/SpareListQueryReq'
import OneClass from '@api/sparepartshospital/model/SpareReq'
import AddClass from '@api/sparepartshospital/model/SpareOperateReq'
import BaseClass from '@api/sparepartshospital/model/SpareDto'

const EditClass = AddClass
const DelClass = OneClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, [BaseClass, 'spare'])
    const resp = await CORE.grpc(TIP, CLIENT, 'querySpareList', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'querySpareDetail', DTO)
    return resp.content || {}
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  transAdd(one) {
    return { ...one, spareCode: '' }
  },
  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, [BaseClass, 'content'])
    await CORE.grpc(TIP, CLIENT, 'insertSpare', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, [BaseClass, 'content'])
    await CORE.grpc(TIP, CLIENT, 'updateSpare', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: [id] })
    await CORE.grpc(TIP, CLIENT, 'deleteSpare', DTO)
    return CORE.grpcOK(TIP)
  },
}
